﻿using System.Net;
using System.Xml;
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Marbles.Core.Web.Syndication
{
    public static class FeedHelper
    {

        public static Type IdentifyFeed(string FeedXml)
        {
            XmlDocument xdoc = new XmlDocument();
            xdoc.LoadXml(FeedXml);
            return IdentifyFeed(xdoc);
        }

        public static Type IdentifyFeed(XmlDocument FeedXml)
        {
            XmlNodeList nodes = FeedXml.GetElementsByTagName("rss");
            if (nodes.Count > 0) { return typeof(RSS.RSSFeed); }

            nodes = null;
            nodes = FeedXml.GetElementsByTagName("feed");
            if (nodes.Count > 0) { return typeof(Atom.AtomFeed); }

            return null;
        }

        public static Type IdentifyFeed(Stream FeedStream)
        {
            StreamReader sw = new StreamReader(FeedStream);
            string xml = sw.ReadToEnd();
            sw.Close();
            return IdentifyFeed(xml);
        }

        public static XmlDocument GetFeedXml(Uri Url, WebProxy Proxy)
        {
            try
            {
                HttpWebRequest req = HttpWebRequest.Create(Url) as HttpWebRequest;
                if (Proxy != null) { req.Proxy = Proxy; }
                HttpWebResponse resp = req.GetResponse() as HttpWebResponse;
                StreamReader sr = new StreamReader(resp.GetResponseStream());
                string xml = sr.ReadToEnd();
                sr.Close();
                resp.Close();

                XmlDocument xdoc = new XmlDocument();
                xdoc.LoadXml(xml);
                return xdoc;
            }
            catch { return null; }
        }

        public static XmlDocument GetFeedXml(Uri Url, string ProxyHost, string Username, string Password)
        {
            try
            {
                HttpWebRequest req = HttpWebRequest.Create(Url) as HttpWebRequest;
                if (ProxyHost != string.Empty)
                {
                    req.Proxy = new WebProxy(ProxyHost, true);
                    req.Proxy.Credentials = new NetworkCredential(Username, Password);
                }
                HttpWebResponse resp = req.GetResponse() as HttpWebResponse;
                StreamReader sr = new StreamReader(resp.GetResponseStream());
                string xml = sr.ReadToEnd();
                sr.Close();
                resp.Close();

                XmlDocument xdoc = new XmlDocument();
                xdoc.LoadXml(xml);
                return xdoc;
            }
            catch { return null; }
        }

        public static XmlDocument GetFeedXml(Uri Url)
        {
            try
            {
                HttpWebRequest req = HttpWebRequest.Create(Url) as HttpWebRequest;
                HttpWebResponse resp = req.GetResponse() as HttpWebResponse;
                StreamReader sr = new StreamReader(resp.GetResponseStream());
                string xml = sr.ReadToEnd();
                sr.Close();
                resp.Close();

                XmlDocument xdoc = new XmlDocument();
                xdoc.LoadXml(xml);
                return xdoc;
            }
            catch { return null; }
        }

        public static XmlDocument GetFeedXml(string Filename)
        {
            if (!File.Exists(Filename)) { return null; }
            FileStream fs = new FileStream(Filename, FileMode.Open, FileAccess.Read, FileShare.Read);
            StreamReader sr = new StreamReader(fs);
            string xml = sr.ReadToEnd();
            sr.Close();
            fs.Close();

            XmlDocument xdoc = new XmlDocument();
            xdoc.LoadXml(xml);
            return xdoc;
        }

        public static string XmlEncode(string value)
        {
            return System.Web.HttpUtility.HtmlEncode(value);
        }

        public static string XmlDecode(string value)
        {
            return System.Web.HttpUtility.HtmlDecode(value);
        }

        internal static XmlAttribute CreateAttribute(XmlDocument Document, string Name, string Value)
        {
            XmlAttribute attr = Document.CreateAttribute(Name);
            attr.Value = Value;
            return attr;
        }

        internal static XmlElement CreateElement(XmlDocument Document, string Name)
        {
            XmlElement node = Document.CreateElement(Name);
            return node;
        }

        internal static XmlElement CreateElement(XmlDocument Document, string Name, string value)
        {
            XmlElement node = Document.CreateElement(Name);
            node.InnerText = value;
            return node;
        }

        internal static XmlElement CreateElement(XmlDocument Document, string Name, string Value, string XmlNs)
        {
            XmlElement node = Document.CreateElement(Name,XmlNs);
            //node.Attributes.Append(Document.CreateAttribute("xmlns", XmlNs));
            node.InnerText = Value;
            return node;
        }

        internal static int ConvertToInt(string value)
        {
            if (value.Trim() == string.Empty) { return 0; }
            int value1 = 0;
            if (!int.TryParse(value.Trim(), out value1)) { value1 = 0; }
            return value1;
        }


        internal static DateTime ConvertToDate(string value)
        {
            if (value.Trim() == string.Empty) { return DateTime.MinValue; }
            DateTime value1 = DateTime.MinValue;
            if (!DateTime.TryParse(value.Trim(), out value1)) { value1 = DateTime.MinValue; }
            return value1;
        }

        internal static Uri ConvertToUri(string value)
        {
            if (value == null) { return null; }
            if (value.Trim() == string.Empty) { return null; }
            Uri value1 = new Uri(value);
            return value1;
        }
    }
}
